<?php
include("controllers/read.php");
$db = openDB("database.sqlite");
$config = getConfig($db);

/******* page edits **********/

if(!$_POST['edit_body'] == "") {
  // get requested title
  $title = $_GET['title'];
  // latest page id
  $latestid['pages'] = sqlite_fetch_single(sqlite_query($db,"SELECT id FROM pages ORDER BY id DESC"))+0;
  // remember author
  setcookie("name",$_POST['edit_author'],time()+60*60*24*30);
  // get links
  $links = array();
  $split = split("}",$_POST['edit_body']);
  for($i=0; $i<count($split)-1; $i++) {
    $segment = $split[$i];
    $split2 = split("{",$segment);
    $linktarget = $split2[1];
    array_push($links,$linktarget);
  }
  // check if page already exists
  if(!sqlite_fetch_array(sqlite_query($db,"SELECT * FROM pages WHERE title LIKE '$title'"))) {
    // NEW PAGE
    // add to pages table
    $id = $latestid['pages']+1;
    // add to pages table
    $query = "INSERT INTO pages (
      id,
      title,
      tags,
      parent
    ) VALUES (
      $id,
      '$title',
      '".sqlite_escape_string(stripslashes($_POST['edit_tags']))."',
      '".sqlite_escape_string(stripslashes($_POST['edit_parent']))."'
    )";
    sqlite_query($db,$query);
  } else {
    // EDIT/REVISION
    // find the page's id
    $id = sqlite_fetch_single(sqlite_query($db,"SELECT id FROM pages WHERE title='$title'"));
    // update tags
    sqlite_query($db,"UPDATE pages SET tags='".$_POST['edit_tags']."' WHERE title LIKE '$title'");
    // update parent
    sqlite_query($db,"UPDATE pages SET parent='".$_POST['edit_parent']."' WHERE title LIKE '$title'");
  }
  // write the new revision
  sqlite_query($db,"INSERT INTO revisions (
    author,
    ip,
    pageid,
    time,
    body
  ) VALUES (
    '".$_POST['edit_author']."',
    '".$_SERVER['REMOTE_ADDR']."',
    $id,
    ".time().",
    '".sqlite_escape_string(stripslashes($_POST['edit_body']))."'
  )");
  // delete existing links from this page
  sqlite_query($db,"DELETE FROM links WHERE fromid = $id");
  // write links
  foreach($links as $link) {
    sqlite_query($db,"INSERT INTO links (
      fromid,
      to
    ) VALUES (
      $id,
      '$link'
    )");
  }
  // go back to the page
  header("location: ".$config['url']."?show=$title");
} else {
  
  /***** config *******/
  
  if(!$_POST['admin_url'] == "") {
    sqlite_query($db,"UPDATE config SET url = '".sqlite_escape_string(stripslashes($_POST['admin_url']))."' WHERE url LIKE 'http://%'");
  }
  if(!$_POST['admin_name'] == "") {
    sqlite_query($db,"UPDATE config SET name = '".sqlite_escape_string(stripslashes($_POST['admin_name']))."' WHERE url LIKE 'http://%'");
  }
  if(!$_POST['admin_tagline'] == "") {
    sqlite_query($db,"UPDATE config SET tagline = '".sqlite_escape_string(stripslashes($_POST['admin_tagline']))."' WHERE url LIKE 'http://%'");
  }
  if(!$_POST['admin_skin'] == "") {
    sqlite_query($db,"UPDATE config SET skin = '".sqlite_escape_string(stripslashes($_POST['admin_skin']))."' WHERE url LIKE 'http://%'");
  }
  header("location: ".$config['url']."?meta=config");
}
?>